package com.sc.nft.dao;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.mapper.DigitalCollectionProductInfoSubscribeMapper;
import com.sc.nft.entity.DigitalCollectionProductInfoSubscribe;
import com.sc.nft.service.DigitalCollectionProductInfoSubscribeService;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 商品申购配置表(DigitalCollectionProductInfoSubscribe)表Dao层
 *
 * @author 王伟成
 * @since 2024-05-13 16:20:42
 */
@Slf4j
@Repository
public class DigitalCollectionProductInfoSubscribeDao extends ServiceImpl<DigitalCollectionProductInfoSubscribeMapper, DigitalCollectionProductInfoSubscribe> {

    public DigitalCollectionProductInfoSubscribe getByProductInfoSubscribe(Long id) {
        LambdaQueryWrapper<DigitalCollectionProductInfoSubscribe> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(DigitalCollectionProductInfoSubscribe::getProductId, id);
        return baseMapper.selectOne(wrapper);
    }
    public List<DigitalCollectionProductInfoSubscribe> drawLots(DateTime offset) {
        LambdaQueryWrapper<DigitalCollectionProductInfoSubscribe> wrapper = Wrappers.lambdaQuery();
        wrapper.lt(DigitalCollectionProductInfoSubscribe::getConsistenceStartTime, offset);
        wrapper.eq(DigitalCollectionProductInfoSubscribe::getIsDraw, Boolean.FALSE);
        return baseMapper.selectList(wrapper);
    }
    /**
     * 该申购商品已抽签
     * @param productId
     */
    public void updateIsDraw(Long productId) {
        LambdaQueryWrapper<DigitalCollectionProductInfoSubscribe> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(DigitalCollectionProductInfoSubscribe::getProductId, productId);
        DigitalCollectionProductInfoSubscribe subscribe = new DigitalCollectionProductInfoSubscribe();
        subscribe.setIsDraw(Boolean.TRUE);
        baseMapper.update(subscribe, wrapper);
    }
    /**
     * 根据商品id修改
     * @param byProductInfoSubscribe
     */
    public Boolean updateByProductId(DigitalCollectionProductInfoSubscribe byProductInfoSubscribe) {
        LambdaQueryWrapper<DigitalCollectionProductInfoSubscribe> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(DigitalCollectionProductInfoSubscribe::getProductId, byProductInfoSubscribe.getProductId());
        return update(byProductInfoSubscribe, wrapper);
    }

    /**
     * 根据商品Id 修改是否发送短信通知状态
     * @param productId
     */
    public void updateNoticeStatusByProductId(Long productId) {
        baseMapper.updateNoticeStatusByProductId(productId);
    }
}
